package org.java8newfeature.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Section9 {
    public static void main(String[] args) {
        Employee e1 = new Employee(1, 23, "M", "Rick", "Beethovan");
        Employee e2 = new Employee(2, 13, "F", "Martina", "Hengis");
        Employee e3 = new Employee(3, 43, "M", "Ricky", "Martin");
        Employee e4 = new Employee(4, 26, "M", "Jon", "Lowman");
        Employee e5 = new Employee(5, 19, "F", "Cristine", "Maria");
        Employee e6 = new Employee(6, 15, "M", "David", "Feezor");
        Employee e7 = new Employee(7, 68, "F", "Melissa", "Roy");
        Employee e8 = new Employee(8, 79, "M", "Alex", "Gussin");
        Employee e9 = new Employee(9, 15, "F", "Neetu", "Singh");
        Employee e10 = new Employee(10, 45, "M", "Naveen", "Jain");

        List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
        System.out.println("打印 age > 20的员工");
        employees.stream().filter(employee -> employee.getAge() > 20).collect(Collectors.toList()).forEach(System.out::println);
        //for 排序
        boolean flag = false;
        for (Employee e : employees) {
            if (e.getAge() > 70) {
                flag = true;
                break;
            }
        }
        System.out.println("使用for each 判断是否有age 大于70的员工" + (flag ? "  有" : "  没有"));

        boolean isAnyMatch = employees.stream().anyMatch(Employee.ageGreaterThan70);
        System.out.println("使用流判断是否有大于79岁的" + (isAnyMatch ? "  有" : "没有"));


        //todo 没有匹配条件的元素，或流为空， 返回true  有的化返回false
        System.out.println(employees.stream().noneMatch(e -> e.getAge() < 8) ? "没有" : "有");

        //todo 年龄大于40的 第一个人
        Optional<Employee> firstEmployee = employees.stream().filter(e -> e.getAge() > 40).findFirst();
        System.out.println(firstEmployee.get());

        Employee employee = employees.stream().filter(e -> e.getAge() > 70).findFirst().orElse(e2);
        System.out.println(employee);

        // TODO: 2024/7/5 过滤年龄小于20的 并返回第一个员工，如果存在就执行输出
        Consumer<Employee> consummer = System.out::println;
        Consumer<Employee> consummer1 = e -> System.out.println(e);
        employees.stream().filter(e -> e.getAge() < 20).findFirst().ifPresent(consummer);


    }


}
